package com.atguigu.ucenterservice.controller;

import com.atguigu.commonutils.R;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户中心控制器
 * @author daygods
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/api/ucenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public R loginUser(@RequestBody UcenterMember member) {
        String token = memberService.login(member);
        return R.ok().data("token", token);
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public R registerUser(@RequestBody UcenterMember member) {
        memberService.register(member);
        return R.ok();
    }

    /**
     * 根据token获取用户信息
     */
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(@RequestHeader("token") String token) {
        UcenterMember member = memberService.getByToken(token);
        return R.ok().data("userInfo", member);
    }
}